<template>
    <van-nav-bar
    :title="(address.id?'编辑':'新增')+'收货地址'"
    left-text="返回"
    left-arrow
    @click-left="onClickLeft"
    />
    <van-address-edit
    :area-list="areaList"
    show-set-default
    :address-info="address"
    :area-columns-placeholder="['请选择', '请选择', '请选择']"
    @save="onSave"
    />
</template>


<script setup>
    import {areaList} from "@vant/area-data"

    const {proxy} = getCurrentInstance()
    const userInfo = proxy.$cookies.get("business")
    const searchResult = ref([{
    }]);
    // 保存
    const onSave = async (values) => {
        let data = {
            busid:userInfo.id?userInfo.id:"",
            consignee:values.name?values.name:"",
            region:values.areaCode?values.areaCode:"",
            address:values.addressDetail?values.addressDetail:"",
            mobile:values.tel?values.tel:"",
            status:values.isDefault?values.isDefault:false,
        }

        // 判断是否存在id，存在则为编辑
        if(address.id){
            data.id = address.id
        }
        // 发送请求
        let result = await proxy.$POST({
            url:"/address/add",
            params:data
        })
        if(result.code){
            proxy.$success({
                message:result.msg,
                duration:1000,
                onClose:()=>{
                    proxy.$router.push(result.url)
                }
            })
        }else{
            proxy.$fail(result.msg)
        }
    };

    // 返回
    const onClickLeft = () => history.back();

    // 编辑地址
    // 获取参数
    let address = reactive(proxy.$route.query)
    if(proxy.$route.query.id){
        address = reactive(proxy.$route.query)
        address.isDefault === "true"?address.isDefault = true:address.isDefault = false
    }

</script>